/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 */

package com.huawei.fastble.adapter;
/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import static ohos.bluetooth.BluetoothRemoteDevice.PAIR_STATE_NONE;
import static ohos.bluetooth.BluetoothRemoteDevice.PAIR_STATE_PAIRED;

import com.huawei.fastble.ResourceTable;
import com.huawei.fastble.bean.BluetoothDevice;
import com.huawei.fastble.slice.MainAbilitySlice;

import ohos.agp.components.*;
import ohos.bluetooth.BluetoothRemoteDevice;
import ohos.hiviewdfx.HiLog;

import java.util.List;

/**
 * BluetoothItemProvider extends BaseItemProvider
 */
public class BluetoothItemProvider extends BaseItemProvider {
    private MainAbilitySlice bluetoothAbilitySlice;

    private List<BluetoothDevice> bluetoothDeviceList;

    public BluetoothItemProvider(MainAbilitySlice bluetoothAbilitySlice, List<BluetoothDevice> itemList) {
        this.bluetoothAbilitySlice = bluetoothAbilitySlice;
        bluetoothDeviceList = itemList;
    }

    private PairOnclickListener listener;

    /**
     * setListener
     * @param listener listener
     */
    public void setListener(PairOnclickListener listener) {
        this.listener = listener;
    }

    @Override
    public int getCount() {
        return bluetoothDeviceList.size();
    }

    @Override
    public Object getItem(int position) {
        return bluetoothDeviceList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        return getRootView(position);
    }

    private Component getRootView(int position) {
        Text deviceName = null;

        Component rootView =
                LayoutScatter.getInstance(bluetoothAbilitySlice)
                        .parse(ResourceTable.Layout_bluetooth_list_item, null, false);

        Component itemComponent = rootView.findComponentById(ResourceTable.Id_bluetooth_device_name);
        if (itemComponent instanceof Text) {
            deviceName = (Text) itemComponent;
        }

        BluetoothDevice bluetoothDevice = bluetoothDeviceList.get(position);
        if (deviceName != null) {
            deviceName.setText(bluetoothDevice.getName());
        }
        Button button = (Button) rootView.findComponentById(ResourceTable.Id_btn_start_pair);
        Text txt_is_pair = (Text) rootView.findComponentById(ResourceTable.Id_txt_is_pair);

        if (bluetoothDevice.getBlueRemoteDevice().getPairState() == PAIR_STATE_NONE) {
            button.setVisibility(Component.VISIBLE);
            txt_is_pair.setText("未配对");
        } else if (bluetoothDevice.getBlueRemoteDevice().getPairState() == PAIR_STATE_PAIRED) {
            txt_is_pair.setText("已配对");
            button.setVisibility(Component.HIDE);
        } else {
            txt_is_pair.setText("配对中…");
            button.setVisibility(Component.VISIBLE);
        }

        button.setClickedListener(
                component -> {
                    if (bluetoothDevice.getPairState() == PAIR_STATE_NONE) {
                        listener.click(bluetoothDevice);
                    }
                });
        return rootView;
    }

    /**
     * updates available Bluetooth devices in UI
     *
     * @param devices list of Bluetooth devices
     */
    public void updateDeviceList(List<BluetoothDevice> devices) {
        bluetoothDeviceList = devices;
        notifyDataChanged();
    }

    public interface PairOnclickListener {
        void click(BluetoothDevice bluetoothDevice);
    }
}
